<?php
include 'config.php';

session_start();

if (!session_is_registered(myusername))
{
        header("Location: index.php");
}

?>
<form id="diagForm" action="" method="GET">
	<table>
        	<tr><td>Blade:</td><td><select class="input" id="commandIP" name="ip">
		<?php
			$result = mysql_query("SELECT * FROM devices WHERE device_type != '6640' AND device_type != '6640 01' AND device_type != '6450' AND device_type != '6660 03' AND device_type != '6312' ORDER BY INET_ATON(device_ip)");
				
			while ( $row = mysql_fetch_array($result))
			{
				$ip_address = $row['device_ip'];
				$hostname = $row['device_name'];
				$device_type = $row['device_type'];

				$port_result = mysql_query("SELECT * FROM device_types WHERE device_type_id = '$device_type'");
				$port_count = mysql_fetch_array($port_result);
				$port_count = $port_count['device_dsl_ports'];						

				//TODO:  Show hostname or IP based on users option in profile
				if ($_SESSION['host_or_ip'] == "ip") {
					echo "<option value=\"$ip_address\">$ip_address</option>\n";
				}
				else {
					echo "<option value=\"$ip_address\">$hostname</option>\n";
				}
			}
		?>
			</select></td></tr>
		<tr><td>Port:</td><td id="ports"><select class="input" id="commandPort" name="port">
		<?php
			//TODO:  Have port total grabbed from DB based on device type.
			$port = 1;
			$port_total = 48;				
				
			echo "<option value=\"\"></option>\n";
			while ( $port <= $port_total )
			{
				echo "<option value=\"$port\">$port</option>\n";
				$port = $port+1;
			}
		?>
			<?php//</select><div><a id="showPortPerformance" href="#">Get Stats</a></div></td></tr>?>
		</select></td></tr>
                        <?php //<tr><td></td><td><input type="button" id="diagFormSubmit" value="Send!" onClick="MochaUI.notification('Please Wait...')"/><input style="padding-left: 5px" type="reset" value="cancel"/><input type="checkbox" id="commandDebug" name="debug" value="True" title="Display verbose output"></input></td></tr>?>
               	<tr><td></td><td><input type="button" id="diagFormSubmit" value="Diagnose" /><input type="checkbox" id="diagDebug" name="debug" value="True" title="Display verbose output" /></td></tr>

	</table>
</form>


<script type="text/javascript">

        $('diagFormSubmit').addEvent('click', function(e){
                MochaUI.notification('Running Diagnostic');
		var url = 'pages/diag_occam.php?';
		url=url+'ip='+$('commandIP').value;
		url=url+'&port='+$('commandPort').value;
		url=url+'&cmd=occam_copper_diag';
		if ($('diagDebug').checked) {
			url=url+'&debug='+$('diagDebug').value;
		}
		url=url+'&nocache='+Math.random();
		MochaUI.updateContent({
                        'element': $('panel1'),
                        'loadMethod': 'xhr',
                        'url': url,
                        'title': 'Results',
                        'padding': { top: 8, right: 8, bottom: 8, left: 8 }
                });
        	showSpinner();
	});

	$('commandIP').addEvent('change', function(e){
		if ($('commandPort').value != ""){ 
			var url = 'pages/blade_info.php?';
			url=url+'ip='+$('commandIP').value;
			url=url+'&port='+$('commandPort').value;
			url=url+'&cmd=modem_status';
			url=url+'&nocache='+Math.random();
			MochaUI.updateContent({
				'element': $('panel2'),
				'loadMethod': 'xhr',
				'url': url,
				'title': 'Port Information',
				'padding': { top: 8, right: 8, bottom: 8, left: 8 }
			});
			showSpinner();
		}
	});

	$('commandPort').addEvent('change', function(e){
		var url = 'pages/blade_info.php?';
		url=url+'ip='+$('commandIP').value;
		url=url+'&port='+$('commandPort').value;
		url=url+'&cmd=modem_status';
		url=url+'&nocache='+Math.random();
		MochaUI.updateContent({
			'element': $('panel2'),
			'loadMethod': 'xhr',
			'url': url,
			'title': 'Port Information',
			'padding': { top: 8, right: 8, bottom: 8, left: 8 }
		});
		showSpinner();
	});

</script>
